package com.st.warehouse.ui.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.st.warehouse.R;
import com.st.warehouse.bean.Student;

import java.util.List;

public class StudentListAdapter extends BaseAdapter {
    private Context mContext;//上下文
    private List<Student> studentList;//数据

    //构造方法
    public StudentListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    //设置数据
    public void setData( List<Student> studentList){
        this.studentList = studentList;
    }

    //加载新的数据
    public void addData(List<Student> newData) {
        studentList.addAll(newData);
    }

    @Override
    public int getCount() {
        return studentList.size();
    }

    @Override
    public Object getItem(int i) {
        return studentList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;

        // 1.推断是不是第一次进来
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_stu_listview,null);
            holder = new ViewHolder();
            holder.stuNo = convertView.findViewById(R.id.stuNo);  //
            holder.stuName = convertView.findViewById(R.id.stuName);  //
            convertView.setTag(holder);//标记,能够复用
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        //2.填充数据
        Student student = studentList.get(position);
        holder.stuNo.setText(student.getStuNo());  //学号
        holder.stuName.setText(student.getStuName());  //姓名

        //3.返回convertView
        return convertView;
    }

    //创建ViewHolder,定义item_stu_listview.xml中的组件
    public final class ViewHolder {
        TextView stuNo;    //
        TextView stuName;  //

    }



}
